package com.lei.lucene;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

/**
 * Lucene测试类
 * 
 * @author Spinacia
 * 
 */
public class LuceneTest {

	// 创建lucene索引
	@Test
	public void createIndex() throws Exception {

		// 创建一个目录存放索引文件
		Directory dir = FSDirectory.open(new File("F:\\JAVA\\luceneIndexs"));

		// 文件分析器
		Analyzer analyzer = new SimpleAnalyzer();

		// 配置文件
		IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);

		// 创建一个写索引对象
		IndexWriter indexWriter = new IndexWriter(dir, conf);

		//获取原始文件
		File files = new File("F:\\JAVA\\就业班视频\\lucene_day01\\资料\\searchsource");
		for (File f : files.listFiles()) {

			// 文件名
			String fileName = f.getName();

			// 内容
			String content = FileUtils.readFileToString(f);

			// 大小
			Long fileSize = FileUtils.sizeOf(f);

			// 路径
			String filePath = f.getPath();

			// 文件名域
			Field fileNameField = new TextField("filename", fileName, Store.YES);

			// 文件内容域
			Field fileContentField = new TextField("filecontent", content,
					Store.NO);

			// 文件大小域
			Field fileSizeField = new LongField("filesize", fileSize, Store.YES);

			// 文件路径域
			Field filePathField = new StoredField("filepath", filePath);

			// 创建一个文档
			Document document = new Document();
			document.add(fileNameField);
			document.add(fileContentField);
			document.add(fileSizeField);
			document.add(filePathField);

			// 添加文档
			indexWriter.addDocument(document);
		}
		// 释放资源
		try {
			indexWriter.close();
		} catch (Exception e) {
			indexWriter = null;
			e.printStackTrace();
		}
	}

	// 查询索引
	@Test
	public void searchIndex() throws Exception {

		// 得到索引所在的位置
		Directory directory = FSDirectory.open(new File(
				"F:\\JAVA\\luceneIndexs"));

		// 创建一个读索引对象
		IndexReader indexReader = DirectoryReader.open(directory);

		// 创建一个索引搜索对象
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);

		Term t = new Term("filename", "apache");
		Query query = new TermQuery(t);
		TopDocs topDocs = indexSearcher.search(query, 10);
		System.out.println("查询到的总记录数:" + topDocs.totalHits);
		for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
			Document document = indexSearcher.doc(scoreDoc.doc);
			System.out.println(document.get("filename"));
			System.out.println(document.get("content"));
			System.out.println(document.get("filepath"));
			System.out.println(document.get("filesize"));
		}

		// 释放资源
		try {
			indexReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
